const tableColumns = [ //表头
    {field: 'id', title: 'ID', type: 'checkbox', fixed: 'left'}
    ,{field: 'id',title: 'ID'}
    ,{field: 'userName',title: '用户名'}
    ,{field: 'email',title: '邮件'}
    ,{field: 'createTime',title: '创建时间', width:200, title: '创建时间', sort: true, minWidth: 100,templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
    ,{field: 'updateTime',title: '更新时间', width:200, title: '更新时间', sort: true, minWidth: 100,templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
    ,{field: 'isDelete',title: '封禁状态',templet:function(d){
            if(d.isDelete)
                return '封号中'
            else
                return '正常'
        }}
    ,{title: '操作' , width:150,field: 'right',templet :'#action_row'}

]
const name = 'userName';

layui.use(['table','form','layer','jquery'],function (table,form,layer,$){

    //region 渲染表格
    table.render({
        id:'LAY-TABLE'
        ,elem: '#LAY_TABLE'
        ,height: 312
        ,url: rootPath //数据接口
        ,height:'full-85'
        ,page: true //开启分页
        ,toolbar:'#toolbar'
        ,cols: [
            tableColumns
        ]
    });
    //endregion

    //region 执行查询操作
    form.on('submit(search)',rs =>{
        //点击查询按钮时使用表单的数据重载表格数据
        table.reload('LAY-TABLE', {
            where: rs.field
        })
        return false;
    })
    //endregion

    // region 删除单个记录
    table.on("tool(TABLE_ACTION)",e=> {
        if(e.event=='del'){
            layer.confirm(`是否确认删除[${e.data[name]}]${modelName}?`,()=>{
                deleteByIds([e.data.id]).then((error,re)=>{
                    layer.msg("删除成功",{icon:6});
                    e.del();
                }).catch((error,rs)=>{
                    layer.msg("删除失败",{icon:5})
                });
            })
        }else if(e.event=='updade'){
            layer.open({
                title:`处理${modelName}`,
                type:2,
                content:`${rootPath}/${e.data.id}`,
                area:["300px","350px"]
            })
        } else if(e.event=='roleMapping'){
            layer.open({
                title:`关联角色`,
                type:2,
                content:`${rootPath}/role/${e.data.id}`,
                area:["500px","600px"]
            })
        }
    })
    //endregion

    // region 头部工具栏操作
    table.on("toolbar(TABLE_ACTION)",e=>{
        if(e.event =='del'){
            let checkStatus = table.checkStatus("LAY-TABLE");
            layer.confirm(`是否删除选中的${checkStatus.data.length}个${modelName}?`,e=>{
                deleteByIds(checkStatus.data.map(account=>account.id)).then((error,re)=>{
                    layer.msg("删除成功",{icon:6});
                    table.reload('LAY-TABLE',{},true)
                }).catch((error,rs)=>{
                    layer.msg("删除失败",{icon:5});
                });
            })
        } else if(e.event == 'add') {
            layer.open({
                title:`新增${modelName}`,
                type:2,
                content:`${rootPath}/add`,
                area:["400px","320px"]
            })
        }

    })
    //endregion

    //region 删除记录操作
    function  deleteByIds(ids) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: `${rootPath}`,
                type: 'delete',
                dataType:'json',
                data:{ids:ids},
                success(rs){
                    if(rs.code ==0 ) {
                        resolve(false,rs)
                    } else {
                        reject(new Error("删除失败"),rs)
                    }
                }, error(e) {
                    reject(e)
                }
            })
        })

    }
    //endregion
});